# Prometheus告警规则配置文件
# 此文件定义各种系统和应用程序的告警规则
# 参考：https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/

groups:
  # === 系统资源告警组 ===
  - name: system.rules
    rules:
      # 高CPU使用率告警
      - alert: HighCPUUsage
        expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > {{ prometheus_alerts.high_cpu_threshold | default(80) }}
        for: {{ prometheus_alerts.high_cpu_duration | default('5m') }}
        labels:
          severity: warning
          service: system
        annotations:
          summary: "High CPU usage detected on {{ '{{ $labels.instance }}' }}"
          description: "CPU usage is above {{ prometheus_alerts.high_cpu_threshold | default(80) }}% (current value: {{ '{{ $value }}' }}%) on {{ '{{ $labels.instance }}' }} for more than {{ prometheus_alerts.high_cpu_duration | default('5m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-cpu-usage"

      # 高内存使用率告警
      - alert: HighMemoryUsage
        expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > {{ prometheus_alerts.high_memory_threshold | default(85) }}
        for: {{ prometheus_alerts.high_memory_duration | default('5m') }}
        labels:
          severity: warning
          service: system
        annotations:
          summary: "High memory usage detected on {{ '{{ $labels.instance }}' }}"
          description: "Memory usage is above {{ prometheus_alerts.high_memory_threshold | default(85) }}% (current value: {{ '{{ $value }}' }}%) on {{ '{{ $labels.instance }}' }} for more than {{ prometheus_alerts.high_memory_duration | default('5m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-memory-usage"

      # 磁盘空间不足告警
      - alert: DiskSpaceUsage
        expr: (1 - (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"})) * 100 > {{ prometheus_alerts.disk_usage_threshold | default(85) }}
        for: {{ prometheus_alerts.disk_usage_duration | default('5m') }}
        labels:
          severity: critical
          service: system
        annotations:
          summary: "Disk space usage is high on {{ '{{ $labels.instance }}' }}"
          description: "Disk usage is above {{ prometheus_alerts.disk_usage_threshold | default(85) }}% (current value: {{ '{{ $value }}' }}%) on {{ '{{ $labels.instance }}' }} mount point {{ '{{ $labels.mountpoint }}' }} for more than {{ prometheus_alerts.disk_usage_duration | default('5m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/disk-space-usage"

      # 磁盘空间严重不足告警
      - alert: DiskSpaceCritical
        expr: (1 - (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"})) * 100 > {{ prometheus_alerts.disk_critical_threshold | default(95) }}
        for: {{ prometheus_alerts.disk_critical_duration | default('1m') }}
        labels:
          severity: critical
          service: system
        annotations:
          summary: "Critical disk space usage on {{ '{{ $labels.instance }}' }}"
          description: "Disk usage is critically high at {{ '{{ $value }}' }}% on {{ '{{ $labels.instance }}' }} mount point {{ '{{ $labels.mountpoint }}' }}. Immediate action required!"
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/disk-space-critical"

      # 高磁盘I/O等待告警
      - alert: HighDiskIOWait
        expr: irate(node_cpu_seconds_total{mode="iowait"}[5m]) * 100 > {{ prometheus_alerts.high_iowait_threshold | default(20) }}
        for: {{ prometheus_alerts.high_iowait_duration | default('5m') }}
        labels:
          severity: warning
          service: system
        annotations:
          summary: "High disk I/O wait on {{ '{{ $labels.instance }}' }}"
          description: "Disk I/O wait is {{ '{{ $value }}' }}% on {{ '{{ $labels.instance }}' }} for more than {{ prometheus_alerts.high_iowait_duration | default('5m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-disk-iowait"

  # === 服务可用性告警组 ===
  - name: service.rules
    rules:
      # 服务下线告警
      - alert: ServiceDown
        expr: up == 0
        for: {{ prometheus_alerts.service_down_duration | default('1m') }}
        labels:
          severity: critical
          service: monitoring
        annotations:
          summary: "Service {{ '{{ $labels.job }}' }} is down"
          description: "Service {{ '{{ $labels.job }}' }} on {{ '{{ $labels.instance }}' }} has been down for more than {{ prometheus_alerts.service_down_duration | default('1m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/service-down"

      # Prometheus目标丢失告警
      - alert: PrometheusTargetMissing
        expr: up == 0
        for: {{ prometheus_alerts.target_missing_duration | default('5m') }}
        labels:
          severity: critical
          service: prometheus
        annotations:
          summary: "Prometheus target missing"
          description: "A Prometheus target has disappeared. An exporter might be crashed. Target: {{ '{{ $labels.instance }}' }} Job: {{ '{{ $labels.job }}' }}"
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/prometheus-target-missing"

  # === 网络告警组 ===
  - name: network.rules
    rules:
      # 高网络接收错误率告警
      - alert: HighNetworkReceiveErrors
        expr: increase(node_network_receive_errs_total[5m]) > {{ prometheus_alerts.network_errors_threshold | default(10) }}
        for: {{ prometheus_alerts.network_errors_duration | default('5m') }}
        labels:
          severity: warning
          service: network
        annotations:
          summary: "High network receive errors on {{ '{{ $labels.instance }}' }}"
          description: "Network interface {{ '{{ $labels.device }}' }} on {{ '{{ $labels.instance }}' }} has received {{ '{{ $value }}' }} errors in the last 5 minutes."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-network-errors"

      # 高网络传输错误率告警
      - alert: HighNetworkTransmitErrors
        expr: increase(node_network_transmit_errs_total[5m]) > {{ prometheus_alerts.network_errors_threshold | default(10) }}
        for: {{ prometheus_alerts.network_errors_duration | default('5m') }}
        labels:
          severity: warning
          service: network
        annotations:
          summary: "High network transmit errors on {{ '{{ $labels.instance }}' }}"
          description: "Network interface {{ '{{ $labels.device }}' }} on {{ '{{ $labels.instance }}' }} has transmitted {{ '{{ $value }}' }} errors in the last 5 minutes."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-network-errors"

  # === 应用程序告警组 ===
  - name: application.rules
    rules:
      # HTTP响应时间过长告警
      - alert: HTTPResponseTime
        expr: histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le, instance, job)) > {{ prometheus_alerts.http_response_time_threshold | default(1) }}
        for: {{ prometheus_alerts.http_response_time_duration | default('5m') }}
        labels:
          severity: warning
          service: webapp
        annotations:
          summary: "High HTTP response time on {{ '{{ $labels.instance }}' }}"
          description: "95th percentile HTTP response time is {{ '{{ $value }}' }}s on {{ '{{ $labels.instance }}' }} for more than {{ prometheus_alerts.http_response_time_duration | default('5m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-http-response-time"

      # HTTP错误率过高告警
      - alert: HighHTTPErrorRate
        expr: (sum(rate(http_requests_total{status=~"5.."}[5m])) by (instance, job) / sum(rate(http_requests_total[5m])) by (instance, job)) * 100 > {{ prometheus_alerts.http_error_rate_threshold | default(5) }}
        for: {{ prometheus_alerts.http_error_rate_duration | default('5m') }}
        labels:
          severity: critical
          service: webapp
        annotations:
          summary: "High HTTP error rate on {{ '{{ $labels.instance }}' }}"
          description: "HTTP error rate is {{ '{{ $value }}' }}% on {{ '{{ $labels.instance }}' }} for more than {{ prometheus_alerts.http_error_rate_duration | default('5m') }}."
          runbook_url: "{{ prometheus_alerts.runbook_base_url | default('') }}/high-http-error-rate"

{% if prometheus_custom_alert_groups is defined %}
{% for group in prometheus_custom_alert_groups %}
  # === {{ group.name }} 自定义告警组 ===
  - name: {{ group.name }}
    rules:
{% for rule in group.rules %}
      - alert: {{ rule.alert }}
        expr: {{ rule.expr }}
{% if rule.for is defined %}
        for: {{ rule.for }}
{% endif %}
        labels:
{% for key, value in rule.labels.items() %}
          {{ key }}: {{ value }}
{% endfor %}
        annotations:
{% for key, value in rule.annotations.items() %}
          {{ key }}: "{{ value }}"
{% endfor %}
{% endfor %}
{% endfor %}
{% endif %}